<?php

class AdController extends Controller{

    private $M_ads;
    private $M_adtimes;
	private $M_adtypes;

    /**
     * 构造方法
     * 初始化
     */
    public function __construct(){
        $this->M_ads = Model::getInstance('ads');
		$this->M_adtimes = Model::getInstance('ad times');
        $this->M_adtypes = Model::getInstance('ad types');
    }
    //模块首页
    public function index(){
        global $controller;

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);
        $title = $this->input()->post('title');
        $ad_type_id = $this->input()->post('ad_type_id');
        $start_time = $this->input()->post('start_time');
        $end_time = $this->input()->post('end_time');
        $filter = array();

        if($title){
            $filter['title'] = $title;
        }

        if($ad_type_id){
            $filter['ad_type_id'] = $ad_type_id;
        }

        if($start_time){
            $filter['start_time'] = $start_time;
        }

        if($end_time){
            $filter['end_time'] = $end_time;
        }

        $ad_type = $this->M_adtypes->getArrayByAdTypeList(0,4);

        $list = $this->M_ads->getAdList($curr_page,$per_page,$filter);

        $data = array(
            'controller'=>$controller,
            'ad_type'=>$ad_type,
            'list'=>$list
        );

        Template::show('ad/index.php',$data);
    }

    //添加
    public function add(){
        global $controller;

        $ad_type = $this->M_adtypes->getArrayByAdTypeList(0,4);

        $data = array(
            'controller'=>$controller,
            'ad_type'=>$ad_type
        );

        Template::show('ad/add.php',$data);
    }

    //添加
    public function schedule(){
        global $controller;

        $ad_id = $this->input()->get('ad_id');

        $info = $this->M_ads->get($ad_id);

        $schedule_info = $this->M_adtimes->getTimesByAdId($ad_id);

        $data = array(
            'controller'=>$controller,
            'info'=>$info,
            'schedule_info'=>$schedule_info
        );

        Template::show('ad/schedule.php',$data);
    }

    //删除
    public function del(){
        $ad_id = $this->input()->get('ad_id');

		$info = $this->M_ads->get($ad_id);

		$ad_image_path = explode('Upload',$info['picture']);

		$ad_image_path = DOCUMENT_ROOT_PATH . 'Upload' . $ad_image_path[1];

		if(file_exists($ad_image_path)){
		   if(!unlink($ad_image_path)){
				$data = array(
					"statusCode"=>"300",
					"message"=>"删除推广位图片失败",
				);
				echo Response::JSON(300,$data);
				exit;
		   }
		}


        $ret = $this->M_ads->delAd($ad_id);

        if($ret){
            $data = array(
                "statusCode"=>"200",
                "message"=>"删除成功",
                "navTabId"=>"ad"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON(300,$data);
        }
    }
	
	public function addManager(){
		global $controller;

        $ad_type = $this->M_adtypes->getArrayByAdTypeList(0,4);

        $data = array(
            'controller'=>$controller,
            'ad_type'=>$ad_type
        );

        Template::show('ad/managertype.php',$data);
	}

    //修改
    public function edit(){
        global $controller;

        $ad_id = $this->input()->get('ad_id');

        $info = $this->M_ads->get($ad_id);

        $ad_type = $this->M_adtypes->getArrayByAdTypeList(0,4);

        $data = array(
            'controller'=>$controller,
            'info'=>$info,
            'ad_type'=>$ad_type
        );

        Template::show('ad/edit.php',$data);
    }

    public function adPictureUpload(){
        $type = $this->input()->get('type','ad');
        $ret = uploadFile($type);

        if($ret['statusCode'] == 2){//上传成功
            $img_ret = explode('Upload',  $ret['data'][0]['savepath'].$ret['data'][0]['savename']);

            echo '/Upload' . $img_ret[1];
        }else{
            echo 'error#'.$ret['data'][0];
        }
    }

    //异步添加
    public function ajaxAdd(){

        $status = $this->input()->post('status');
        $title = $this->input()->post('title');
        $summary = $this->input()->post('summary');
        $ad_type_id = $this->input()->post('ad_type_id');
        $link = $this->input()->post('link');
        $start_time = $this->input()->post('start_time');
        $end_time = $this->input()->post('end_time');
        $picture = $this->input()->post('picture',null);
        $user_id = $this->session()->get('user_id',rand(13698,13714));
        $note = $this->input()->post('note');

        if(!$status || !$title || !$ad_type_id || !$link || !$start_time || !$end_time || !$user_id){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误，请填写完入库表单！",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_ads->addAd($ad_type_id, $title, $summary, $link, $picture, 0, $user_id, $start_time, $end_time, $status, $note);
        if($ret){

            $data = array(
                "statusCode"=>"200",
                //"message"=>"添加成功",
                //"navTabId"=>"ad",
                //"callbackType"=>"closeCurrent",
				"ad_id"=>$ret
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"添加失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }

    //异步添加
    public function ajaxEdit(){

        $ad_id = $this->input()->post('ad_id');
        $sort = $this->input()->post('sort');
        $status = $this->input()->post('status');
        $title = $this->input()->post('title');
        $ad_type_id = $this->input()->post('ad_type_id');
        $link = $this->input()->post('link');
        $start_time = $this->input()->post('start_time');
        $end_time = $this->input()->post('end_time');
        $picture = $this->input()->post('picture');
        $user_id = $this->session()->get('user_id',rand(13698,13714));

        if(!$ad_id || !$status || !$title || !$ad_type_id || !$link || !$start_time || !$end_time || !$picture || !$user_id){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误，请填写完入库表单！",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_ads->updateAd($ad_id, $ad_type_id, $title, $link, $picture, $sort, $user_id, $start_time, $end_time, $status);

        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"编辑成功",
                "navTabId"=>"ad",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"编辑失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }

    public function ajaxManagerType(){
        $manager_type_id = (int)$this->input()->post('manager_type_id');
        $parent_ad_type_id = (int)$this->input()->post('parent_ad_type_id');
        $type_name = $this->input()->post('type_name');

        if(!$manager_type_id || !$type_name ){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误，请填写完入库表单！",
            );
            echo Response::JSON(300,$data);
            exit;
        }
        $show_str = '';
        switch($manager_type_id){
            case 1://添加
            case 2://添加
                $ret = $this->M_adtypes->addAdType($type_name,$parent_ad_type_id);
                $show_str='添加';
                break;
            case 3://修改
            case 4://添加
                $ret = $this->M_adtypes->updateAdType($parent_ad_type_id,$type_name);
                $show_str='修改';
                break;
            case 5://删除
                $ret = $this->M_adtypes->delAllAdType($parent_ad_type_id);
                $show_str='删除';
                break;
            default:
                $data = array(
                    "statusCode"=>"300",
                    "message"=>"操作类型选择错误，请刷新页面后重试！",
                );
                echo Response::JSON(300,$data);
                exit;
                break;
        }

        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>$show_str."推广类型成功",
                "navTabId"=>"ad",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>$show_str."推广类型失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }

    public function ajaxGetType(){
        $manager_type_id = (int)$this->input()->get('manager_type_id');

        switch($manager_type_id){
            case 1://添加父类
                $ret = array(array(0,'请正确填写一下类型'));
                break;
            case 3://修改父类
                $ret = $this->M_adtypes->getArrayByAdTypeList(0,1);
                break;
            case 2://添加子类
                $ret = $this->M_adtypes->getArrayByAdTypeList(0,1);
                break;
            case 4://修改子类
                $ret = $this->M_adtypes->getArrayByAdTypeList(0,2);
                break;
            case 5://删除
                $ret = $this->M_adtypes->getArrayByAdTypeList(0,3);
                break;
            default:
                $data = array(
                    "statusCode"=>"300",
                    "message"=>"操作类型选择错误，请刷新页面后重试！",
                );
                echo Response::JSON(300,$data);
                exit;
                break;
        }

        echo Response::Default_JSON(0,$ret);
    }
}